﻿// Sheego.Service.X400
// Copyright(C) 2015 Schwab Versand GmbH
//
// This source file is subject to the Microsoft Public License(Ms-PL).
// http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.
using Sheego.Service.X400.Job.Shared.BO;
using Sheego.Service.X400.Job.Shared.Comparer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Sheego.Service.X400.Job.Impl.Comparer
{
    class X400AddressComparer : IX400AddressComparer
    {
        #region IX400AddressComparer Members

        public bool AreEqual(IX400Address AddressA, IX400Address AddressB)
        {
            if (AddressA == null)
                throw new ArgumentNullException("AddressA");

            if (AddressB == null)
                throw new ArgumentNullException("AddressB");

            return GetDifferences(AddressA, AddressB).Count == 0 ? true : false;
        }

        #endregion


        private static List<PropertyInfo> GetDifferences(IX400Address AddressA, IX400Address AddressB)
        {
            List<PropertyInfo> differences = new List<PropertyInfo>();
            foreach (PropertyInfo property in AddressA.GetType().GetProperties())
            {
                var stringValue1 = (String)property.GetValue(AddressA, null);
                var stringValue2 = (String)property.GetValue(AddressB, null);

                var lvalue1 = stringValue1.ToLower();
                var lvalue2 = stringValue2.ToLower();

                if (!lvalue1.Equals(lvalue2))
                {
                    differences.Add(property);
                }
            }
            return differences;
        }
    }
}
